package com.axend.vetwavve.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.axend.lib_base.base.vm.BaseViewModel
import com.axend.lib_room.entity.PetInfoEntity
import com.axend.lib_room.manager.PetInfoManager
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.logging.Logger
import javax.inject.Inject

@HiltViewModel
class PetRecordViewModel @Inject constructor() : BaseViewModel() {

    private var _petInfoList: MutableLiveData<MutableList<PetInfoEntity>> = MutableLiveData()
    val petInfoList: LiveData<MutableList<PetInfoEntity>> get() = _petInfoList

    /**
     * 总条目
     */
    private var _totalCount: MutableLiveData<Int> = MutableLiveData()
    val totalCount: LiveData<Int> get() = _totalCount

    /**
     * 页码数
     */
    private var _pageNum: MutableLiveData<Int> = MutableLiveData(0)
    val pageNum: LiveData<Int> get() = _pageNum

    /**
     * 搜索模式
     */
    private var _searchMode: MutableLiveData<Boolean> = MutableLiveData(false)
    val searchMode: LiveData<Boolean> get() = _searchMode


    /**
     * 设置页码数
     */
    fun setPageNum(pageNum: Int) {
        _pageNum.value = pageNum
    }

    /**
     * 设置搜索模式
     */
    fun setSearchMode(searchMode: Boolean) {
        _searchMode.value = searchMode
    }



    /**
     * 模拟插入数据
     */
    fun insertPetInfoList() {
        launchOnUI {
            val list = mutableListOf<PetInfoEntity>()
            for (i in 0..50) {
                list.add(
                    PetInfoEntity()
                )
            }
            PetInfoManager.savePetInfoList(list)
        }
    }


    /**
     * 获取所有宠物档案列表
     */
    fun getPetInfoList() {
        launchOnUI {
            loadStartLoading()
            val results = withContext(Dispatchers.IO) { PetInfoManager.getAllPetInfoList() }
            _petInfoList.postValue(results as MutableList<PetInfoEntity>)
            loadSuccess()
        }
    }


    /**
     * 删除宠物档案
     */
    fun deletePetInfoItem(petInfoEntity: PetInfoEntity) {
        launchOnUI {
            withContext(Dispatchers.IO) {
                PetInfoManager.deletePetInfoItem(petInfoEntity)
            }
        }
    }


    /**
     * 模糊查询档案数据
     */
    fun searchPetInfoList(keyword: String="", pageSize: Int, offset: Int,delay: Long=0L) {
        launchOnUI {
            try {
                val results = withContext(Dispatchers.IO) {
                    delay(delay)
                    PetInfoManager.getPetInfoListByName(keyword, pageSize, offset)
                }
                _petInfoList.postValue(results as MutableList<PetInfoEntity>)
            } catch (e: Exception) {
                Log.e("PetRecordViewModel", e.message.toString())
            }
        }
    }

    /**
     * 模糊查询所有数据
     */
    fun searchAllPetInfoList(keyword: String) {
        launchOnUI {
            try {
                val results = withContext(Dispatchers.IO) {
                    PetInfoManager.getPetInfoListByName(keyword)
                }
                _petInfoList.postValue(results as MutableList<PetInfoEntity>)
            } catch (e: Exception) {
                Log.e("PetRecordViewModel", e.message.toString())
            }
        }
    }

    /**
     * 分页查询所有数据
     */
    fun getPetInfoListByPage(pageSize: Int, offset: Int) {
        launchOnUI {
            try {
                val results = withContext(Dispatchers.IO) {
                    PetInfoManager.getPetInfoListByPage(pageSize, offset)
                }
                _petInfoList.postValue(results as MutableList<PetInfoEntity>)
            } catch (e: Exception) {
                Log.e("PetRecordViewModel", e.message.toString())
            }
        }
    }

    /**
     * 获取宠物档案条目数
     */
    fun getPetInfoCount(keyword: String,delay:Long=0L) {
        launchOnUI {
            try {
                val count = withContext(Dispatchers.IO) {
                    delay(delay)
                    PetInfoManager.getPetInfoCount(keyword)
                }
                _totalCount.postValue(count)
            } catch (e: Exception) {
                Log.e("PetRecordViewModel", e.message.toString())
            }
        }
    }
}